package com.bzsg.rightsManagementSystem.dao

import com.bzsg.rightsManagementSystem.pojo.PersonUser
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface UserDao {
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("select * from personUser where id = #{id}")
    fun queryById(id: Int): PersonUser?

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Select("select * from personUser limit #{offset}, #{limit}")
    fun queryAllByLimit(@Param("offset") offset: Int, @Param("limit") limit: Int): List<PersonUser>?

    /**
     * 通过实体作为筛选条件查询
     *
     * @param user 实例对象
     * @return 对象列表
     */

    @Select("   select  *  from personUser")
    fun queryAll(): List<PersonUser>?

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 影响行数
     */
    @Insert(
        "  insert into personUser(account, user_name, password, last_login_time, enabled, account_non_expired,\n" +
                "                         account_non_locked, credentials_non_expired, create_time, update_time, create_user,\n" +
                "                         update_user)\n" +
                "        values (#{account}, #{userName}, #{password}, #{lastLoginTime}, #{enabled}, #{accountNonExpired},\n" +
                "                #{accountNonLocked}, #{credentialsNonExpired}, #{createTime}, #{updateTime}, #{createUser},\n" +
                "                #{updateUser})"
    )
    fun insert(user: PersonUser)

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 影响行数
     */
    @Update(
        "     <script>\n" +
                "        update personUser\n" +
                "        <set>\n" +
                "            <if test=\"account != null and account != ''\">\n" +
                "                account = #{account},\n" +
                "            </if>\n" +
                "            <if test=\"userName != null and userName != ''\">\n" +
                "                user_name = #{userName},\n" +
                "            </if>\n" +
                "            <if test=\"password != null and password != ''\">\n" +
                "                password = #{password},\n" +
                "            </if>\n" +
                "            <if test=\"lastLoginTime != null\">\n" +
                "                last_login_time = #{lastLoginTime},\n" +
                "            </if>\n" +
                "            <if test=\"enabled != null\">\n" +
                "                enabled = #{enabled},\n" +
                "            </if>\n" +
                "            <if test=\"accountNonExpired != null\">\n" +
                "                account_non_expired = #{accountNonExpired},\n" +
                "            </if>\n" +
                "            <if test=\"accountNonLocked != null\">\n" +
                "                account_non_locked = #{accountNonLocked},\n" +
                "            </if>\n" +
                "            <if test=\"credentialsNonExpired != null\">\n" +
                "                credentials_non_expired = #{credentialsNonExpired},\n" +
                "            </if>\n" +
                "            <if test=\"createTime != null\">\n" +
                "                create_time = #{createTime},\n" +
                "            </if>\n" +
                "            <if test=\"updateTime != null\">\n" +
                "                update_time = #{updateTime},\n" +
                "            </if>\n" +
                "            <if test=\"createUser != null\">\n" +
                "                create_user = #{createUser},\n" +
                "            </if>\n" +
                "            <if test=\"updateUser != null\">\n" +
                "                update_user = #{updateUser},\n" +
                "            </if>\n" +
                "        </set>\n" +
                "        where id = #{id}\n" +
                "          </script>"
    )
    fun update(user: PersonUser)

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    @Delete("delete from  personUser  where id = #{id}")
    fun deleteById(id: Int)

    /**
     * 根据用户名查询用户
     *
     * @param userName
     * @return 实例对象
     */
    @Select("select * from personUser where user_name = #{userName}")
    fun selectUserByName(userName: String): PersonUser?

    /**
     * 根据账户名查找用户
     *
     * @param account
     * @return 实例对象
     */
    @Select("select * from personUser where account = #{account}")
    fun selectUserByAccount(account: String): PersonUser?

    /**
     * 查询用户数量
     *
     * @param
     * @return 用户数量
     */
    @Select("SELECT COUNT(*) FROM personUser")
    fun selectTotalUser(): Int
}